import '../../model/model.dart';
import 'model_default.dart';

extension SignModelExtension on SignModel {
  SignModel get copy {
    return SignModel(freq, name, index,
        gain: gain,
        Q: Q,
        min_gain: min_gain,
        max_gain: max_gain,
        min_Q: min_Q,
        max_Q: max_Q,
        select: select,
        enable: enable);
  }
}

extension PassModelExtension on Passmodel {
  Passmodel get copy {
    return Passmodel(name, ishigh, freq,
        min_freq: min_freq,
        max_freq: max_freq,
        select: select,
        enable: enable,
        isBypass: isBypass,
        type: type?.copy);
  }

  void reset() {
    freq = ishigh ? min_freq : max_freq;
    isBypass = false;
    type!.reset();
  }
}

extension PassTypeExtension on PassType {
  PassType get copy {
    return PassType(name, slope, slopeList: slopeList);
  }

  void reset() {
    name = 'Bessel';
    slope = slopeList[0];
  }
}

extension ChannelModelExtension on ChannelModel {
  SignModel get select_sign {
    return sign_list.firstWhere(($0) => $0.select, orElse: () => default_sign);
  }

  ChannelModel get copy {
    return ChannelModel(this.num,
        name: name,
        mute: mute,
        phase: phase,
        db: db,
        max_db: max_db,
        min_db: min_db,
        select: select,
        enable: enable,
        hex_line: hex_line,
        delay: delay,
        input_list: input_list,
        input_assign_list: input_assign_list,
        input_custom_list: input_custom_list,
        high_pass: high_pass?.copy,
        low_pass: low_pass?.copy,
        sign_list: sign_list.map(($0) => $0.copy));
  }
}

extension GlobalModelExtension on GlobalModel {
  List<ChannelModel> get select_ch_list {
    List<ChannelModel> _list = ch_list.where(($0) => $0.select).toList();
    return _list.isNotEmpty ? _list : [ch_list[0]];
  }

  GlobalModel get copy {
    return GlobalModel(tag,
        mute: mute,
        db: db,
        min_db: min_db,
        max_db: max_db,
        isLock: isLock,
        enable: enable,
        date: date,
        input_type: input_type,
        copy_stype: copy_stype,
        ch_list: ch_list.map(($0) => $0.copy).toList());
  }

  ///所有 通道 ，转换成默认值
  void reset() {
    mute = false;
    db = max_db;
    input_type = '';
    copy_stype = '';
    for (var ch in ch_list) {
      ch.mute = false;
      ch.phase = false;
      ch.db = ch.max_db;
      ch.delay = '0';
      ch.delay_type = 'ms';
      List<int> list = List.filled(8, 0);
      ch.input_list.clear();
      ch.input_list.addAll(list);
      ch.input_custom_list.clear();
      ch.input_custom_list.addAll(list);
      ch.high_pass!.reset();
      ch.low_pass!.reset();
      for (var i = 0; i < ch.sign_list.length; i++) {
        SignModel sign = ch.sign_list[i];
        sign.index = i;
        sign.gain = 0;
        sign.Q = 7.2;
        sign.freq = sign_freq_list[i];
        sign.name = '${sign.freq}';
      }
    }
  }
}
